package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshGridView;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.ClassifyLiveListAdapter;
import com.v1.video.domain.ClassifyVRLiveEntity;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

/**
 * 分类直播列表
 * 
 * @author wangfang
 * 
 */
public class ClassifyLiveListActivity extends BaseActivity implements
		OnClickListener {

	private final String TAG = "ClassifyLiveListActivity";
	private View mEmptyView;
	private TextView mTvTitle;
	private PullToRefreshGridView mRefreshGridView;
	private GridView mGridView;
	private ClassifyLiveListAdapter mAdapter;
	private String mScId;
	private int mCurIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_classifylivelist);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mEmptyView = getLayoutInflater().inflate(R.layout.fragment_no_content,
				null);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mRefreshGridView = (PullToRefreshGridView) this
				.findViewById(R.id.gv_gridView);
		mRefreshGridView.setScrollingWhileRefreshingEnabled(false);
		mRefreshGridView.setMode(Mode.PULL_FROM_START);
		mGridView = mRefreshGridView.getRefreshableView();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		mEmptyView.setOnClickListener(this);
		mRefreshGridView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				mCurIndex = 0;
				if (Helper.checkConnection(ClassifyLiveListActivity.this)) {
					getServerData();
				} else {
					if (mRefreshGridView != null) {
						mRefreshGridView.onRefreshComplete();
					}
					showToast(ClassifyLiveListActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(ClassifyLiveListActivity.this)) {
					getServerData();
				} else {
					if (mRefreshGridView != null) {
						mRefreshGridView.onRefreshComplete();
					}
					showToast(ClassifyLiveListActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.layout.fragment_no_content:
			if (mRefreshGridView != null) {
				mRefreshGridView.doPullRefreshing(100);
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		String scname = intent.getStringExtra("scname");
		mScId = intent.getStringExtra("scId");
		if (!TextUtils.isEmpty(scname)) {
			mTvTitle.setText(scname);
		} else {
			mTvTitle.setText("");
		}
		mAdapter = new ClassifyLiveListAdapter(this,mGridView);
		mGridView.setAdapter(mAdapter);
		if (mRefreshGridView != null) {
			mRefreshGridView.doPullRefreshing(100);
		}
	}

	/**
	 * 设置空的界面显示
	 */
	private void setEmptyView() {
		if (mCurIndex == 0) {
			View view = mGridView.getEmptyView();
			if (view == null) {
				mGridView.setEmptyView(mEmptyView);
			}
			if (mRefreshGridView != null) {
				mRefreshGridView.setMode(Mode.PULL_FROM_START);
			}
		}
	}

	/**
	 * 获取服务器数据
	 */
	private void getServerData() {
		String url = String.format(Constant.CLASSIFY_VR_LIVE_LIST, mScId,
				mCurIndex, Constant.HOME_WATERFULL_PAGE_SIZE,
				Constant.PRODUCT_CODE, Constant.PRODUCT_VERSION,
				Constant.DEVICEID);
		Logger.i(TAG, "VR分类直播url==" + url);
		RequestManager.getInstance().getRequest(this, url,
				ClassifyVRLiveEntity.class, mScId, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						if (mRefreshGridView != null) {
							mRefreshGridView.onRefreshComplete();
						}
						if (object != null) {
							ClassifyVRLiveEntity entity = (ClassifyVRLiveEntity) object;
							if (entity != null && entity.getBody() != null
									&& entity.getBody().getData() != null) {
								if (mAdapter != null) {
									if (mCurIndex == 0) {
										mAdapter.setLstData(entity.getBody()
												.getData());
									} else {
										mAdapter.setAddLstData(entity.getBody()
												.getData());
									}
								}
								if ((mCurIndex + 1) < entity.getBody()
										.getPage()) {
									mCurIndex++;
									if (mRefreshGridView != null) {
										mRefreshGridView.setMode(Mode.BOTH);
									}
								} else {
									if (mRefreshGridView != null) {
										mRefreshGridView
												.setMode(Mode.PULL_FROM_START);
									}
								}
							} else {
								setEmptyView();
							}
						} else {
							setEmptyView();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						if (mRefreshGridView != null) {
							mRefreshGridView.onRefreshComplete();
						}
						setEmptyView();
						Logger.i(TAG, message);
					}

				});
	}

}
